package com.jeesite.modules.ic.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.NumberUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.http.HttpClientUtils;
import com.jeesite.modules.ic.dao.IcUserDao;
import com.jeesite.modules.ic.entity.IcCompany;
import com.jeesite.modules.ic.service.IcCompanyService;
import com.jeesite.modules.mo.entity.MoLiushui;
import com.jeesite.modules.mo.entity.MoUserDevice;
import com.jeesite.modules.mo.service.MoLiushuiService;
import com.jeesite.modules.mo.service.MoUserDeviceService;
import com.jeesite.modules.rt.entity.RtDailiDevice;
import com.jeesite.modules.rt.service.RtDailiDeviceService;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.utils.Common;
import com.jeesite.modules.utils.tupian.PicUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcUserService;

/**
 * 用户表Controller
 * @author 李寒冰
 * @version 2024-06-21
 */
@Controller
@RequestMapping(value = "${adminPath}/ic/icUser")
public class IcUserController extends BaseController {

	@Autowired
	private IcUserService icUserService;
    @Autowired
    private IcCompanyService icCompanyService;

	@Autowired
	private IcUserDao icUserDao;
    @Autowired
    private MoLiushuiService moLiushuiService;

	@Value("${pic.baseUrl}")
	private String houtaiPicBaseUrl;

	@Value("${pic.host}")
	private String qianduanPicUrl;
    @Autowired
    private RtDailiDeviceService rtDailiDeviceService;
    @Autowired
    private MoUserDeviceService moUserDeviceService;

	@Value("${meiti.host}")
	private String meitiHost;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public IcUser get(String id, boolean isNewRecord) {
		return icUserService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ic:icUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(IcUser icUser, Model model) {
		model.addAttribute("icUser", icUser);
		if(StringUtils.isNotEmpty(icUser.getParentUserId())){
			return "modules/ic/icUserParentList";
		}
		return "modules/ic/icUserList";
	}

	/**
	 *
	 */
	@RequiresPermissions("ic:icUser:view")
	@RequestMapping(value = {"meitiUser", ""})
	@ResponseBody
	public String meitiUser(IcUser icUser) {
		String isMeiti = icUser.getIsMeiti();
		icUser = icUserService.get(icUser.getId());
		if(icUser.getIsMeiti().equals(isMeiti)){
			return renderResult(Global.TRUE, text("保存用户表成功！"));
		}
		if(StringUtils.isNotEmpty(isMeiti)){
			if(DictUtils.getDictValue("sys_yes_no","是","1").equals(isMeiti)){
				icUser.setIsMeiti(DictUtils.getDictValue("sys_yes_no","是","1"));
			}else{
				icUser.setIsMeiti(DictUtils.getDictValue("sys_yes_no","否","0"));
			}
		}

		String resultJSON = "";
		//同步到媒体系统
		if(isMeiti.equals("1")){
			resultJSON = HttpClientUtils.get(meitiHost+"/js/api/user/createMeitiZhanghu?mobile="+icUser.getMobile());
		}else{
			resultJSON = HttpClientUtils.get(meitiHost+"/js/api/user/disableMeitiZhanghu?meitiUserCode="+icUser.getMeitiUserCode());
		}
		if(StringUtils.isNotEmpty(resultJSON)){
			icUserService.save(icUser);
			return renderResult(Global.TRUE, text("操作成功！"));
		}else{
			return renderResult(Global.FALSE, text("操作失败！"));
		}
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ic:icUser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<IcUser> listData(IcUser icUser, HttpServletRequest request, HttpServletResponse response) {
		icUser.setPage(new Page<>(request, response));
		Page<IcUser> page = icUserService.findPage(icUser);
		List<IcUser> list = page.getList();
		for (int i = 0; i < list.size(); i++) {
			IcUser icUser1 = list.get(i);
			if(StringUtils.isEmpty(icUser1.getAvatarUrl())){
				icUser1.setAvatarUrl(Common.avatarUrlSet(icUser1.getAvatarUrl(),houtaiPicBaseUrl));
			}

			IcCompany userCompany = icUserDao.getUserCompany(icUser1.getId());
			MoLiushui moLiushui = new MoLiushui();
			moLiushui.setUserId(icUser1.getId());
			List<MoLiushui> list1 = moLiushuiService.findList(moLiushui);
			double sum = 0;
			for (int j = 0; j < list1.size(); j++) {
				MoLiushui moLiushui1 = list1.get(j);
				Double jine = moLiushui1.getJine();
				sum = NumberUtils.add(sum, jine);
			}
			icUser1.setYue(sum);

			if(userCompany!=null){
				icUser1.setCompanyName(userCompany.getName());
			}
			String parentUserId = icUser1.getParentUserId();
			if(StringUtils.isNotEmpty(parentUserId)){
				icUser1.setParentUser(icUserService.get(parentUserId));
			}
			IcUser icUser2 = new IcUser();
			icUser2.setParentUserId(icUser1.getId());
			icUser2.setStatus("0");
			long count = icUserService.findCount(icUser2);
			icUser1.setXiajiNum(new Long(count).intValue());
			String avatarUrl = icUser1.getAvatarUrl();
			icUser1.setAvatarUrl(PicUtil.zhuanbianPic(avatarUrl,qianduanPicUrl));

			//分销设备数
			RtDailiDevice rtDailiDevice = new RtDailiDevice();
			rtDailiDevice.setUserId(icUser1.getId());
			long count1 = rtDailiDeviceService.findCount(rtDailiDevice);
			icUser1.setFenxiaoDevice(new Long(count1).intValue());

			//设备数量
			MoUserDevice moUserDevice = new MoUserDevice();
			moUserDevice.setUserId(icUser1.getId());
			long countDevice = moUserDeviceService.findCount(moUserDevice);
			icUser1.setDeviceNum(new Long(countDevice).intValue());

			//注册时间
			Date loginTime = icUser1.getLoginTime();
			if(loginTime!=null){
				double distanceOfTwoDate = DateUtils.getDistanceOfTwoDate(loginTime,new Date());
				icUser1.setWeidenglvDay(new Double(distanceOfTwoDate).intValue());
			}
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ic:icUser:view")
	@RequestMapping(value = "form")
	public String form(IcUser icUser, Model model) {
		IcCompany icCompany = new IcCompany();
		List<IcCompany> list = icCompanyService.findList(icCompany);
		model.addAttribute("listCompany", list);
		model.addAttribute("icUser", icUser);
		return "modules/ic/icUserForm";
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ic:icUser:view")
	@RequestMapping(value = "set")
	public String set(IcUser icUser, Model model) {
		model.addAttribute("icUser", icUser);
		return "modules/ic/icUserSet";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ic:icUser:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated IcUser icUser) {
		icUserService.save(icUser);
		return renderResult(Global.TRUE, text("保存用户表成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("ic:icUser:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(IcUser icUser) {
		icUser.setStatus(IcUser.STATUS_DISABLE);
		icUserService.updateStatus(icUser);
		return renderResult(Global.TRUE, text("停用用户表成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("ic:icUser:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(IcUser icUser) {
		icUser.setStatus(IcUser.STATUS_NORMAL);
		icUserService.updateStatus(icUser);
		return renderResult(Global.TRUE, text("启用用户表成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ic:icUser:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(IcUser icUser) {
		icUserService.delete(icUser);
		return renderResult(Global.TRUE, text("删除用户表成功！"));
	}
	
}